package com.yjd.api.project;

import com.yjd.comm.base.control.BaseControl;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.model.IntroDetailModel;
import com.yjd.comm.base.model.PagerModel;
import com.yjd.comm.base.service.IIntroDetailService;
import com.yjd.comm.project.model.ProjectCaseModel;
import com.yjd.comm.project.service.IProjectCaseService;
import com.yjd.comm.util.ResultMapBuilder;
import com.yjd.comm.util.ServletUtil;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 案例相关操作
 * Created by yangchangyan on 2017/7/13.
 */
@Controller("com.yjd.api.project.CaseControl")
@Scope("prototype")
@RequestMapping("/case")
public class CaseControl extends BaseControl {
    @Resource
    private IProjectCaseService projectCaseService;

    @Resource
    private IIntroDetailService introDetailService;

    /**
     * 查询案例
     * Created by yangchangyan on 2017/7/13.
     * @param ref_type 案例类型 1机构、2项目、3医生
     * @param ref_id 关联的id
     * @return void
     * @throws
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public void queryCase(int ref_type, Long ref_id) throws Exception {
        Map<String, Object> paramMap = ServletUtil.getParameterMap(request);
        paramMap.put("caseList", this.queryCase(paramMap));
        this.successData(paramMap);
    }

    private List<Map<String, Object>> queryCase(Map<String, Object> queryParamMap) throws Exception {
        List<Map<String, Object>> cases = new ArrayList<>();
        PagerModel orgPage = projectCaseService.getPageModel(queryParamMap, DbWREnums.READ);
        ProjectCaseModel pcm = null;
        IntroDetailModel detail = null;
        for(Object obj:orgPage.getDataList()){
            pcm = (ProjectCaseModel)obj;
            Map<String, Object> caseResult = ResultMapBuilder.build(pcm);
            detail = introDetailService.getModelById(pcm.getDetail_id(), DbWREnums.READ);
            caseResult.put("detail", null == detail?"":detail.getContent());
            cases.add(caseResult);
        }
        return cases;
    }
}
